package com.bupt.cad.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bupt.cad.entity.Com;
import com.bupt.cad.entity.ComType;
import com.bupt.cad.mapper.ComDao;
import com.bupt.cad.service.IScreenToBlueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScreenToBlueService implements IScreenToBlueService {

    @Autowired
    ComDao comDao;
    @Override
    public List<Com> getAllBlueComps(int screenId) {
        List<Com> comps = comDao.getCompByComTypeAndScreenId(screenId,ComType.screenToBlueCom);
        if(comps==null)return null;
        return comps;
    }

    @Override
    public boolean insertBlueComps(JSONObject object) {
        int screenId = (int) object.get("screenId");
        String json = JSON.toJSONString(object.get("blueComps"));
        List<Com> comps = JSON.parseObject(json, new TypeReference<List<Com>>() {});
        if(screenId==0|| comps ==null)return false;
        comDao.deleteCompByComTypeAndScreenId(screenId,ComType.screenToBlueCom);
        if(comps.size()==0)return true;
        for(Com com: comps){
            com.setScreenId(screenId);
            com.setComType(ComType.screenToBlueCom);
        }
        int i = comDao.insertComps(comps);
        if(i!=0)return true;
        return false;
    }
}
